package xmlS
{
	import main.XMLLoaderBase;
	
	import struct.CityStruct;
	
	public class CityListXMLLoader extends XMLLoaderBase
	{
		private var xmlData:XML;
		
		private var delayFuction:Function;
		private var objectList:Array = [];
		
		public function CityListXMLLoader()
		{
		}
		
		public function initThis(func:Function = null):void
		{
			delayFuction = func;
			loadXml(uri.getSysXml("CityList"),saveData,loadingThis);
		}
		
		private function saveData(data:XML = null):void
		{
			if(data){
				xmlData = data;
				changeAndSave();
			}
			
			if(delayFuction is Function){
				delayFuction();
				delayFuction = null;
			}
		}
		
		private function changeAndSave():void
		{
			objectList = [];
			
			var list:Array = changeListToArray(xmlData.city);//xmlData.shop;
			for(var i:int = 0;i < list.length;i++){
				var obj:ScenicDesign = new ScenicDesign;
				obj.init(list[i],uri);
				var scenicStruct:CityStruct = new CityStruct();
				scenicStruct.initWithData(obj);
				objectList.push(scenicStruct);
			}
		}
		
		private function changeListToArray(list:XMLList):Array
		{
			var valueList:Array = [];
			
			for each(var xml:XML in list){
				valueList.push(xml);
			}
			
			return valueList;
		}
		
		private function loadingThis():void
		{
			trace("正在拉取城市");
		}
		
		public function load(keyName:*,keynum:* = null):*
		{
			var value:*;
			
			if(objectList.length == 0){
				initThis(function():void{
					value = load(keyName,keynum)
				});
			}else{
				value = getValue(keyName,keynum);
			}
			
			return value;
		}
		
		public function get spotList():Array
		{
			return objectList;
		}
		
		private function getValue(keyName:*,keyNum:* = null):*
		{
			var info:Object;
			
			if(keyNum != null){
				for(var i:int = 0;i < objectList.length;i++){
					if(keyName is Array && keyNum is Array){
						if(objectList[i][keyName[0]] && objectList[i][keyName[0]] == keyNum && objectList[i][keyName[1]] && objectList[i][keyName[1]] == keyNum){
							info = objectList[i]
							return info;
						}
					}else{
						if(objectList[i][keyName] && objectList[i][keyName] == keyNum){
							info = objectList[i];
							return info;
						}
					}
				}
				return null;
			}else{
				return objectList;
			}
		}
	}
}

import assists.URI;

import main.ClientConstants;

class ScenicDesign
{
	public var id:String = "";
	public var type:int = 0;
	public var cityName:String = "";
	public var weather:Number = 0;
	public var fileName:String = "";
	public var childrenList:*;
	
	public function ScenicDesign()
	{
	}
	
	public function init(xml:XML,uri:URI):void
	{
		this.id = xml.id;
		this.type = xml.type;
		this.cityName = xml.cityName;
		this.weather = xml.weather;
		this.fileName = xml.fileName;
		this.childrenList = xml.childrenList;
	}
}
